package com.java83.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java83.dto.impl.OneDto;
import com.java83.entity.Employee;
import com.java83.response.ResponseEntity;
import com.java83.service.EntryManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: EntryManagementController
 * @Description: EntryManagementController(Web)控制层
 * @Author: TangShuJi
 * @Date: 2021/12/14 14:36
 * @Version: V1.0
 **/
@Controller
public class EntryManagementController {

    @Autowired
    @Qualifier(value = "entryManagementService")
    private EntryManagementService entryManagementService;

    /**
     * @Author: TangShuJi
     * @Description: (Web控制层)添加员工方法   要在Service修改对应部门人数
     * @Date: 2021/12/14 20:14
     * @Param: [employee] 需要被添加的员工
     * @Return: com.java83.response.ResponseEntity<java.lang.String>
     **/
    @RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> addEmployee(@RequestBody Employee employee) {
        int num = entryManagementService.add(employee);
        if (num == 1) {
            // 添加成功
            return new ResponseEntity<String>(200, "添加成功", String.valueOf(num));
        } else {
            // 添加失败
            return new ResponseEntity<String>(401, "添加失败，服务器正在维护中", null);
        }
    }

    /**
     * @Author: TangShuJi
     * @Description: (Web控制层)通过id删除员工方法
     * @Date: 2021/12/14 17:29
     * @Param: [id] 需要被删除的员工id
     * @Return: com.java83.response.ResponseEntity<java.lang.String>
     **/
    @RequestMapping(value = "/deleteEmployee/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResponseEntity<String> deleteEmployee(@PathVariable("id") int id) {
        int num = entryManagementService.deleteEmployee(id);
        if (num == 1) {
            // 删除成功
            return new ResponseEntity<String>(200, "删除成功", String.valueOf(num));
        } else {
            // 删除失败
            return new ResponseEntity<String>(401, "删除失败，服务器正在维护中", null);
        }
    }

    /**
     * @Author: TangShuJi
     * @Description: (Web控制层)修改员工方法
     * @Date: 2021/12/14 17:40
     * @Param: [employee] 需要被修改的员工
     * @Return: com.java83.response.ResponseEntity<java.lang.String>
     **/
    @RequestMapping(value = "/updateEmployee", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> updateEmployee(@RequestBody Employee employee) {
        int num = entryManagementService.updateEmployee(employee);
        if (num == 1) {
            // 修改成功
            return new ResponseEntity<String>(200, "修改成功", String.valueOf(num));
        } else {
            // 修改失败
            return new ResponseEntity<String>(401, "修改失败，服务器正在维护中", null);
        }
    }

    /**
     * @Author: TangShuJi
     * @Description: (Web控制层)查询所有员工方法，带分类
     * @Date: 2021/12/14 16:41
     * @Param: [employeeOneDto] 需要分页的参数和查询的员工信息整体的DTO实体对象
     * @Return: com.java83.response.ResponseEntity<com.github.pagehelper.PageInfo<com.java83.entity.Employee>>
     **/
    @RequestMapping(value = "/findAllEmployee", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<PageInfo<Employee>> findAllEmployee(@RequestBody OneDto<Employee> employeeOneDto) {
        System.out.println(employeeOneDto.getPage() + "..." + employeeOneDto.getLimit());
        PageHelper.startPage(employeeOneDto.getPage(), employeeOneDto.getLimit());
        List<Employee> employeeList = entryManagementService.findAllEmployee(employeeOneDto.getData());
        PageInfo<Employee> employeePageInfo = new PageInfo<Employee>(employeeList);
        ResponseEntity<PageInfo<Employee>>  responseEntity = new ResponseEntity<>(200, "成功", employeePageInfo);
        return responseEntity;
    }


}
